function loadItemResult() {
    radItemId   =   $('#radTicketItemListLbx').val();
    if(radItemId == null) {
        return;
    }
    showAjaxLoader('ajaxLoader', 'dummy', true);
    $.ajax({
            type:    "GET",
            url:      "../Endoscopy/getItemResultAjax?set_ajax_view",
            data:     "radTicketItemId=" + radItemId,
            dataType: "json",
            complete: function(){
                showAjaxLoader('ajaxLoader', 'dummy', false);
            },
            success:    function(data) {
                if (data.error.code == 'OK') {
                    //$('#serviceName').text(data['result']['serviceName']);
                    setItemResultData(data.result);
                    $('#uploadRadImageBtn').removeAttr('disabled');
                    $('#saveRadResultBtn').removeAttr('disabled');
                    $('#radTicketItemId').val(radItemId);
                }
                else {
                    displayError(data.error.mess);              
            }
        }
    });
}

function setItemResultData(data) {
    $('#serviceName').text(data.serviceName);
    $('#machine').val(data.machineName);
    $('#doctorId').val(data.doctorId);
    $('#technicianId1').val(data.technician1);
    $('#technicianId2').val(data.technician2);
    $('#description').val(data.description);
    $('#conclusion').val(data.conclusion);
    $('#additionalServiceCol').html(data.additionalService);
    if (data.created_dd != null && data.created_mm != null && data.created_yyyy != null) {
        $('#created_dd').val(data.created_dd);
        $('#created_mm').val(data.created_mm);
        $('#created_yyyy').val(data.created_yyyy);
        $('#createdTime').val(data.createdTime);
    }
    else {
        var currentTime = new Date();
        var month   =   currentTime.getMonth() + 1;
        var day     =   currentTime.getDate();
        var year    =   currentTime.getFullYear();
        var hour    =   currentTime.getHours();
        var minutes =   currentTime.getMinutes();
        $('#created_dd').val(day);
        $('#created_mm').val(month);
        $('#created_yyyy').val(year);
        $('#createdTime').val(hour + ":" + minutes);
    }
    $('#radImageListDiv').html(data.radImage);
    $('#radImageListDiv a.gallery').lightBox({
	   imageLoading: '../images/lightbox-ico-loading.gif',
	   imageBtnClose: '../images/lightbox-btn-close.gif',
	   imageBtnPrev: '../images/lightbox-btn-prev.gif',
	   imageBtnNext: '../images/lightbox-btn-next.gif',
	   imageBlank: '../images/lightbox-blank.gif'
	});
}
function saveMetaData() {
    if (!isReadyToSubmitRadMeta()) {
        return;
    }
    showAjaxLoader('ajaxLoader', 'dummy', true);
    radItemId   =   $('#radTicketItemListLbx').val();
    commonData  =   $('#commonDataFrm').formSerialize();
    conclusion  =   $('#conclusionFrm').formSerialize();
    $.ajax({
           type:    "POST",
           url:      "../Endoscopy/saveMetaDataAjax?set_ajax_view",
           data:     "radTicketItemId=" + radItemId + "&" + commonData + "&" + conclusion,
           dataType: "json",
           complete: function(){
               showAjaxLoader('ajaxLoader', 'dummy', false);
           },
           success:    function(data) {
               if (data.error.code == 'OK') {
                    showMessage('radResultMsg', _("Saving succeed"), 'success');
                    setTimeout("clearText('radResultMsg')", 10000);
                    $('#radTicketItemListLbx option[value=\'' + data.result.itemId + '\']').attr('label', data.result.itemName);
                    $('#radTicketItemListLbx option[value=\'' + data.result.itemId + '\']').text(data.result.itemName);
               }
               else {
                   displayError(data.error.mess);              
           }
       }
    });
}

function isReadyToSubmitRadMeta(){
    if (!validateSelectBox('radTicketItemListLbx', _('Please select a item to edit data'), 'radResultMsg')) {
        return false;
    }
    
    if (!validateSelectBox('doctorId', _('Not yet selected doctor'), 'radResultMsg')) {
        return false;
    }
    
    if (!checkFullDate($('#created_yyyy'), $('#created_mm'), $('#created_dd'), true, _('Invalid date'), 'radResultMsg', false)) {
        return false;
    }
    
    if (!checkHour($('#createdTime'), true, _('Invalid time'), 'radResultMsg')) {
        return false;
    }
    
    if (!validateSelectBox('roomId', _('Not yet selected executive place'), 'radResultMsg')) {
        return false;
    }
    
    return true;
}

function uploadImage(){
    showAjaxLoader('ajaxLoader', 'dummy', true);
    $('#uploadRadImageFrm').submit();
}

function showUploadResult(data, status) {
	if (data.error.code == 'OK') {
		$('#radImageListTbl tbody tr:first').append(data.result);
		showAjaxLoader('ajaxLoader', 'dummy', false);
	}
	else {
	    displayError(data.error.mess);              
	}
}

function deleteRadImage(radImageId) {
     $.ajax({
           type:    "GET",
           url:      "../RadResult/deleteRadImageAjax?set_ajax_view",
           data:     "radImageId=" + radImageId,
           dataType: "json",
           complete: function(){
               
           },
           success:    function(data) {
               if (data.error.code == 'OK') {
                    $('td#' + radImageId).remove(); 
               }
               else {
                   displayError(data.error.mess);              
           }
       }
    });
}

function loadItemResultView() {
    radItemId   =   $('#radTicketItemListLbx').val();
    if(radItemId == null) {
        return;
    }
    showAjaxLoader('ajaxLoader', 'dummy', true);
    $.ajax({
            type:    "GET",
            url:      "../Endoscopy/getItemResultViewAjax?set_ajax_view",
            data:     "radTicketItemId=" + radItemId,
            dataType: "json",
            complete: function(){
                showAjaxLoader('ajaxLoader', 'dummy', false);
            },
            success:    function(data) {
                if (data.error.code == 'OK') {
                    setItemResultViewData(data.result);
                }
                else {
                    displayError(data.error.mess);              
            }
        }
    });
}

function setItemResultViewData(data) {
    $('#serviceName').text(data.serviceName);
    $('#machine').text(data.machineName);
    $('#doctorName').text(data.doctorName);
    $('#technicianId1').text(data.technician1);
    $('#technicianId2').text(data.technician2);
    $('#description').html(data.description);
    $('#conclusion').html(data.conclusion);
    $('#additionalServiceCol').html(data.additionalService);
    $('#created').text(data.created);
    $('#radImageListDiv').html(data.radImage);
    $('#radImageListDiv a.gallery').lightBox({
       imageLoading: '../images/lightbox-ico-loading.gif',
       imageBtnClose: '../images/lightbox-btn-close.gif',
       imageBtnPrev: '../images/lightbox-btn-prev.gif',
       imageBtnNext: '../images/lightbox-btn-next.gif',
       imageBlank: '../images/lightbox-blank.gif'
    });
}